<?php

/* * ****************************************************************************
 * controllers\Insur_DocController
 * 
 * author  Laureta Xhaferaj
 * email   laureta@first.al
 * 
 * @function index
 * @function create
 * @function store
 * @function show
 * @function edit
 * @function update
 * @function history
 * @function destroy
 * *************************************************************************** */

class Insur_DocController extends \BaseController {
    /*     * ********************************************************************************************************* *
     * Constructor.
     *
     * ******************************************************************************************************** */

    public function __construct() {
        $this->beforeFilter('auth', array('only' => array('index')));
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index($id) {
        
    }

    /**
     * The form for creating a new insur&docs.
     *
     * @return array
     */
    public function create() {
        $cars = DB::table('cars')->orderBy('Description', 'asc')
                        ->distinct()->lists('Description', 'id');
        return View::make('pages.insur_docs_create', array(
                    'cars' => $cars,
        ));
    }

    /**
     * Store a new insur&docs data for a specific vehicle
     *
     * @return Response
     */
    public function store() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'car' => Lang::get('messages.car'),
            'owner' => Lang::get('messages.owner'),
            'ownership_cert' => Lang::get('messages.ownership_cert'),
            'drive_permis' => Lang::get('messages.driving_license'),
            'sgs' => Lang::get('messages.sgs'),
            'tpl' => Lang::get('messages.tpl'),
            'kasko' => Lang::get('messages.kasko'),
        );
        $rules = array(
            "car" => "required|unique:insur_docs,car_id",
            "owner" => "required",
            "ownership_cert" => "required",
            "drive_permis" => "required",
            "sgs" => "required",
            "tpl" => "required",
            "kasko" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);
        $cars = DB::table('cars')->orderBy('Description', 'asc')
                        ->distinct()->lists('Description', 'id');
        if ($validation->passes()) {
            $doc = new InsurDoc();
            $doc->owner = Input::get('owner');
            $doc->ownership_cert = Input::get('ownership_cert');
            $doc->authoriz = Input::get('authoriz');
            $doc->drive_permis = Input::get('drive_permis');
            $doc->sgs = Input::get('sgs');
            $doc->tpl = Input::get('tpl');
            $doc->kasko = Input::get('kasko');
            $doc->inter_permis = Input::get('inter_permis');
            $doc->car_id = Input::get('car');
            $doc->sgs_cost = Input::get('sgs_cost');
            $doc->sgs_tax = Input::get('sgs_tax');
            $doc->tpl_cost = Input::get('tpl_cost');
            $doc->kasko_cost = Input::get('kasko_cost');
            $doc->user_id = Auth::user()->id;
            $doc->save();

            //insert history
            $data = new InsurdocsInsert();
            $data->owner = Input::get('owner');
            $data->ownership_cert = Input::get('ownership_cert');
            $data->authoriz = Input::get('authoriz');
            $data->drive_permis = Input::get('drive_permis');
            $data->sgs = Input::get('sgs');
            $data->tpl = Input::get('tpl');
            $data->kasko = Input::get('kasko');
            $data->inter_permis = Input::get('inter_permis');
            $data->car_id = Input::get('car');
            $data->sgs_cost = Input::get('sgs_cost');
            $data->sgs_tax = Input::get('sgs_tax');
            $data->tpl_cost = Input::get('tpl_cost');
            $data->kasko_cost = Input::get('kasko_cost');
            $data->user_id = Auth::user()->id;
            $data->save();

            return Response::json(["redirect_to" => "/"]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                    'cars' => $cars);
                return Response::json($response_values);
            } else {
                $data['errors'] = $validation->errors();
                $data['cars'] = $cars;
                return View::make('pages.insur_docs_create', $data);
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        //
    }

    /**
     * Edit form for insur & docs data.
     *
     * @param  int  $id
     * @return array
     */
    public function edit($id) {
        $values = DB::table('insur_docs')->where('car_id', $id)->get();
        return View::make('pages.insur_docs', array(
                    'values' => $values,
        ));
    }

    /**
     * Update insur&docs data.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'owner' => Lang::get('messages.owner'),
            'ownership_cert' => Lang::get('messages.ownership_cert'),
            'drive_permis' => Lang::get('messages.driving_license'),
            'sgs' => Lang::get('messages.sgs'),
            'tpl' => Lang::get('messages.tpl'),
            'kasko' => Lang::get('messages.kasko'),
        );
        $rules = array(
            "owner" => "required",
            "ownership_cert" => "required",
            "drive_permis" => "required",
            "sgs" => "required",
            "tpl" => "required",
            "kasko" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);
        if ($validation->passes()) {
            $car_id = DB::select('select car_id from insur_docs where id = ?', array($id));
            $data = InsurDoc::find($id);
            $data->owner = Input::get('owner');
            $data->ownership_cert = Input::get('ownership_cert');
            $data->authoriz = Input::get('authoriz');
            $data->drive_permis = Input::get('drive_permis');
            $data->sgs = Input::get('sgs');
            $data->tpl = Input::get('tpl');
            $data->kasko = Input::get('kasko');
            $data->inter_permis = Input::get('inter_permis');
            $data->sgs_cost = Input::get('sgs_cost');
            $data->sgs_tax = Input::get('sgs_tax');
            $data->tpl_cost = Input::get('tpl_cost');
            $data->kasko_cost = Input::get('kasko_cost');
            $data->save();
            return Response::json(["redirect_to" => '../car/' . $car_id[0]->car_id]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray());
                return Response::json($response_values);
            } else {
                $data['errors'] = $validation->errors();
                return View::make('pages.insur_docs', $data);
            }
        }
    }

    /**
     * Display the history of insur & docs changes.
     *
     * @return Response
     */
    public function history() {
        $history = DB::select('select r.*, u.username from revisions as r '
                        . 'left join users as u on u.id=r.user_id');
        return View::make('pages.history', array(
                    'history' => $history,
        ));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        //
    }

}
